using UnityEngine;
using System.Collections;

/// <summary>
/// Control player's movement, collision detecting etc.
/// </summary>
[RequireComponent(typeof (PathMotor), 
				  typeof (PlayerCameraController),
				  typeof (CharacterController))]
public class PlayerController : MonoBehaviour
{
	/// <summary>
	/// Gets view anchor for camera rendering.
	/// </summary>
	public Vector3 GetViewAnchor ()
	{
		// TMP
		Vector3 viewAnchoXZ = m_pathMotor.positionOnPathXZ;
		Terrain terrain = Terrain.activeTerrain;
		float terrainH = terrain.SampleHeight (viewAnchoXZ);
		viewAnchoXZ.y = terrainH + terrain.gameObject.transform.position.y;
		return viewAnchoXZ;
	}
	
	public Vector3 GetViewForwardXZ ()
	{
		// TMP
		return gameObject.transform.forward.normalized;
	}
	
	void Awake ()
	{
		m_pathMotor = gameObject.GetComponent<PathMotor> ();
		m_playerCamController = gameObject.GetComponent<PlayerCameraController> ();
		CharacterController cc = gameObject.GetComponent<CharacterController> ();
		cc.slopeLimit = 90.0f;
	}
	
	void OnGUI ()
	{
		if (GUI.Button (new Rect (10, 10, 100, 25), "Start"))
        {
            GameObject go = GameObject.Find ("curveHost");
            m_pathMotor.StartMove (go.GetComponent<PathBehaviour> ().bezier);
        }
        
        if (GUI.Button (new Rect (120, 10, 100, 25), "GoLeft"))
        {
            m_pathMotor.GotoLeftTrack ();
        }
        
        if (GUI.Button (new Rect (230, 10, 100, 25), "GoRight"))
        {
            m_pathMotor.GotoRightTrack ();
        }
		
		if (GUI.Button (new Rect (340, 10, 100, 25), "Forward"))
		{
			m_playerCamController.changeViewMode (PlayerViewMode.Forward);
		}
		
		if (GUI.Button (new Rect (450, 10, 100, 25), "Backward"))
		{
			m_playerCamController.changeViewMode (PlayerViewMode.Backward);
		}
		
		if (GUI.Button (new Rect (560, 10, 100, 25), "Horizontal"))
		{
			m_playerCamController.changeViewMode (PlayerViewMode.Horizontal);
		}
		
	}
	
	PathMotor m_pathMotor;
	
	PlayerCameraController m_playerCamController;
}

